package com.qifn.blog.Controller;

import com.github.pagehelper.PageInfo;
import com.qifn.blog.Model.Blog;
import com.qifn.blog.Model.Tag;
import com.qifn.blog.Service.BlogService;
import com.qifn.blog.Service.TagService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.util.List;

@Controller
@RequestMapping("/")
public class TagController {

    @Resource
    TagService tagService;

    @Resource
    BlogService blogService;

    @GetMapping("/tag")
    public ModelAndView tag(Integer tagId,@RequestParam(defaultValue = "1")Integer pageNum){
        ModelAndView mv = new ModelAndView();
        List<Tag> tagList = tagService.queryAllTag();
        if(tagId == null){
            tagId = tagList.get(0).getId();
        }
        PageInfo<Blog> blogPageInfo = blogService.queryBlogByTagId(tagId,pageNum,3);
        mv.addObject("tagId",tagId);
        mv.addObject("tagList",tagList);
        mv.addObject("blogPageInfo",blogPageInfo);
        mv.setViewName("tag");
        return mv;
    }
}
